// TypeScript file

class AutoRotationSetting {
    private context: MainPanel;
    private group: eui.Group;

    private lineBet: NumPanel2;
    private lineNum: NumPanel2;
    private thisMoney: NumPanel2;

    private lineBetArray = [0.01, 0.02, 0.05, 0.1, 0.5, 1, 5, 10];
    private lineNumArray = [25, 50, 75, 100, 125];
    private rotationNumArray = [5, 25, 50, 100, 200];
    private rotationNumIndexMap = {
        "5": 0,
        "25": 1,
        "50": 2,
        "100": 3,
        "200": 4
    };
    private lineBetCurIndex = 0;
    private lineNumCurIndex = 0;
    private rotationNumCurIndex = 5;
    private isInit: boolean = false;

    private lineBetSubBtn: eui.Button;
    private lineBetAddBtn: eui.Button;
    private lineNumSubBtn: eui.Button;
    private lineNumAddBtn: eui.Button;

    private rotation_5: eui.ToggleButton;
    private rotation_25: eui.ToggleButton;
    private rotation_50: eui.ToggleButton;
    private rotation_100: eui.ToggleButton;
    private rotation_200: eui.ToggleButton;
    private rotationArray;

    private rotationBtn: eui.Group;

    private rotationBtnNum:NumPanel2;

    private moreUI:MoreUI;

    private rotationMoreBtn:eui.Button;

    private cumtomRatation:CumtomRotation;

    private autoStopBtn:eui.Group;
    private autoStopBtnNum:eui.BitmapLabel;

    public constructor(context: MainPanel, group: eui.Group,moreUI:MoreUI) {
        if (!this.isInit) {
            this.isInit = true;
            this.context = context;
            this.group = group;
            this.moreUI = moreUI;
            this.lineBet = new NumPanel2(0, 0, "");
            this.lineBet.setScale(1);
            var mark = < eui.Label > this.group.getChildByName("lineBetMark");
            this.lineBet.x = mark.x + mark.width / 2;
            this.lineBet.y = mark.y + mark.height / 2;
            this.lineNum = new NumPanel2(0, 0, "");
            this.lineNum.setScale(1);
            mark = < eui.Label > this.group.getChildByName("lineNumberMark");

            this.lineNum.x = mark.x + mark.width / 2;
            this.lineNum.y = mark.y + mark.height / 2;
            this.thisMoney = new NumPanel2(0, 0, "");
            this.thisMoney.setScale(1);
            mark = < eui.Label > this.group.getChildByName("allBetMark");
            this.thisMoney.x = mark.x + mark.width / 2;
            this.thisMoney.y = mark.y + mark.height / 2;

            this.rotationBtnNum =  new NumPanel2(0, 0, "");
            this.rotationBtnNum.setScale(1);
            this.rotationBtnNum.x = 70;
            this.rotationBtnNum.y = 50;
    
            this.initNum();

            this.lineBetSubBtn = < eui.Button > this.group.getChildByName("lineBetSubBtn");
            this.lineBetSubBtn.enabled = false;
            this.lineBetAddBtn = < eui.Button > this.group.getChildByName("lineBetAddBtn");

            this.lineNumSubBtn = < eui.Button > this.group.getChildByName("lineNumSubBtn");
            this.lineNumSubBtn.enabled = false;
            this.lineNumAddBtn = < eui.Button > this.group.getChildByName("lineNumAddBtn");

            this.rotation_5 = < eui.ToggleButton > this.group.getChildByName("5");
            this.rotation_25 = < eui.ToggleButton > this.group.getChildByName("25");
            this.rotation_50 = < eui.ToggleButton > this.group.getChildByName("50");
            this.rotation_100 = < eui.ToggleButton > this.group.getChildByName("100");
            this.rotation_200 = < eui.ToggleButton > this.group.getChildByName("200");

            this.rotationArray = [this.rotation_5, this.rotation_25, this.rotation_50, this.rotation_100, this.rotation_200];
            this.initRotationButtonState();

            // this.rotationBtn = < eui.Group > this.group.getChildByName("rotationBtn");
            // this.rotationBtn.$setAnchorOffsetX(this.rotationBtn.width/2);
            // this.rotationBtn.$setAnchorOffsetY(this.rotationBtn.height/2);
            // this.rotationBtn.addChild(this.rotationBtnNum);
            // this.rotationBtn.alpha = 0;

            this.rotationMoreBtn = <eui.Button> this.group.getChildByName("rotationMoreBtn");
            this.cumtomRatation = new CumtomRotation(this.moreUI,this);
            this.cumtomRatation.$setVisible(false);
            this.cumtomRatation.x = this.moreUI.width/2 - this.cumtomRatation.width/2;
            this.cumtomRatation.y = this.moreUI.height/2 - this.cumtomRatation.height/2;
            this.moreUI.addChild(this.cumtomRatation);

            
            this.autoStopBtn = <eui.Group> this.group.getChildByName("autoStopBtn");
            this.autoStopBtnNum = <eui.BitmapLabel> this.autoStopBtn.getChildByName("autoStopBtnNum");
            this.autoStopBtn.visible =false;

            this.initEvent();
        }

    }

    private initNum(): void {
        this.group.addChild(this.lineBet);
        this.group.addChild(this.lineNum);
        this.group.addChild(this.thisMoney);

        this.lineBetCurIndex = this.context.crystalPanle.cur_zhu.curNumS;
        this.lineNumCurIndex = this.context.crystalPanle.cur_multiple.curNumS;

       // this.rotationBtnNum.setNum(this.rotationNumArray[this.rotationNumCurIndex]);

        this.setBetLineNumber();
        this.setThisMoney();
        


    }

    private initRotationButtonState() {
        for (var i = 0; i < this.rotationArray.length; i++) {
            if (i == this.rotationNumCurIndex) {
                ( < eui.ToggleButton > this.rotationArray[i]).selected = true;
            } else {
                ( < eui.ToggleButton > this.rotationArray[i]).selected = false;
            }
        }
    }

    private initEvent() {

        this.lineBetAddBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.lineBetAddBtnEvent, this);
        this.lineBetSubBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.lineBetSubBtnEvent, this);
        this.lineNumSubBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.lineNumSubBtnEvent, this);
        this.lineNumAddBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.lineNumAddBtnEvent, this);

        for (var i = 0; i < this.rotationArray.length; i++) {
            ( < eui.ToggleButton > this.rotationArray[i]).addEventListener(egret.TouchEvent.TOUCH_TAP, this.rotationNumButtonEvent, this);
        }

        // this.rotationBtn.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.rotationBtnBeginEvent,this);
        // this.rotationBtn.addEventListener(egret.TouchEvent.TOUCH_END,this.rotationBtnEndEvent,this);

        this.rotationMoreBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.rotationMoreBtnTapEvent,this);

        this.autoStopBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.autoStopBtnBeginEvent,this);
        this.autoStopBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.autoStopBtnEndEvent,this);

    }

    private lineBetAddBtnEvent(evt: egret.Event) {
        if (this.lineBetCurIndex < (this.lineBetArray.length - 1)) {
            this.lineBetSubBtn.enabled = true;
            this.lineBetCurIndex++;
            this.setBetLineNumber();
            this.setThisMoney();
            this.context.crystalPanle.cur_zhu.setNum(this.lineBetCurIndex)
            this.context.crystalPanle.ca_zhu();
            if(this.lineBetCurIndex == (this.lineBetArray.length - 1))
            {
                this.lineBetAddBtn.enabled = false;
            }
        }

    }

    private lineBetSubBtnEvent(evt: egret.Event) {
        if (this.lineBetCurIndex > 0) {
            this.lineBetAddBtn.enabled = true;
            this.lineBetCurIndex--;
            this.setBetLineNumber();
            this.setThisMoney();
            this.context.crystalPanle.cur_zhu.setNum(this.lineBetCurIndex);
            this.context.crystalPanle.ca_zhu();
            if(this.lineBetCurIndex == 0 )
            {
                 this.lineBetSubBtn.enabled = false;
            }
        }
    }

    private lineNumAddBtnEvent(evt: egret.Event) {
        if (this.lineNumCurIndex < (this.lineNumArray.length - 1)) {
            this.lineNumSubBtn.enabled = true;
            this.lineNumCurIndex++;
            this.setBetLineNumber();
            this.setThisMoney();
            this.context.crystalPanle.cur_multiple.setNum(this.lineNumCurIndex);
            this.context.crystalPanle.ca_zhu();
            if(this.lineNumCurIndex == (this.lineNumArray.length - 1))
            {
                this.lineNumAddBtn.enabled = false;
            }
        }

    }

    private lineNumSubBtnEvent(evt: egret.Event) {
        if (this.lineNumCurIndex > 0) {
            this.lineNumAddBtn.enabled = true;
            this.lineNumCurIndex--;
            this.setBetLineNumber();
            this.setThisMoney();
            this.context.crystalPanle.cur_multiple.setNum(this.lineNumCurIndex);
            this.context.crystalPanle.ca_zhu();
            if(this.lineNumCurIndex == 0 )
            {
                 this.lineNumSubBtn.enabled = false;
            }
        }
    }

    private rotationNumButtonEvent(evt: egret.Event) {
        var name = evt.target.name;
        this.rotationNumCurIndex = this.rotationNumIndexMap[name];
        this.initRotationButtonState();
        this.rotationBtnNum.setNum(this.rotationNumArray[this.rotationNumCurIndex]);
        this.rotationBtnNum.y = 70;
        this.moreUI.context.crystalPanle.autoRotationIndex = this.rotationNumCurIndex;
        this.moreUI.context.crystalPanle.autoRotationBtn.setNumber(this.rotationNumArray[this.rotationNumCurIndex]);
        this.moreUI.context.crystalPanle.autoRotationBtn.setAutoAvbleState();
        this.context.gunDeal.gunNumRotate(this.rotationNumArray[this.rotationNumCurIndex]);
        this.moreUI.$setVisible(false);
        this.rotationNumCurIndex = 5;
        this.initRotationButtonState();

    }

    private rotationBtnBeginEvent(evt:egret.Event)
    {
        this.rotationBtn.scaleX = 0.9;
        this.rotationBtn.scaleY = 0.9;
    }

    private rotationBtnEndEvent(evt:egret.Event)
    {
        this.rotationBtn.scaleX = 1;
        this.rotationBtn.scaleY = 1;

    }

    private rotationMoreBtnTapEvent(evt:egret.Event)
    {
        this.cumtomRatation.setNumber();
        this.cumtomRatation.$setVisible(true);
    }






    private setThisMoney() {
        this.thisMoney.setNum(this.lineBet.getNum() * this.lineNum.getNum() * 25);
    }
    private setBetLineNumber(): void {
        this.lineBet.setNum(this.lineBetArray[this.lineBetCurIndex]);
        this.lineNum.setNum(this.lineNumArray[this.lineNumCurIndex]);
    }

    public getThisMoney():number
    {
        return this.thisMoney.getNum();
    }

    public setTouchEvent(enable:boolean,isAuto:boolean):void
    {
        // this.group.touchEnabled = enable;
        // this.group.touchChildren = enable;
        this.lineBetSubBtn.enabled = enable;
        this.lineBetAddBtn.enabled = enable;
        this.lineNumSubBtn.enabled = enable;
        this.lineNumAddBtn.enabled = enable;
        this.rotation_5.enabled = enable;
        this.rotation_25.enabled = enable;
        this.rotation_50.enabled = enable;
        this.rotation_100.enabled = enable;
        this.rotation_200.enabled = enable;
        this.rotationMoreBtn.enabled = enable;
        if(isAuto){
           // this.autoStopBtn.touchEnabled = !enable;
            this.autoStopBtn.visible = !enable;
        }

    }

    public setAutoBtnNum(num:number)
    {
        this.autoStopBtnNum.$setText(num+"");
    }

    private autoStopBtnBeginEvent(evt:egret.Event)
    {
        this.moreUI.context.soundctr.playSound("click_mp3");
        this.autoStopBtn.scaleX = 0.9;
        this.autoStopBtn.scaleY = 0.9;
        egret.setTimeout(function(){
        this.autoStopBtn.scaleX = 1;
        this.autoStopBtn.scaleY = 1;
        this.context.gunDeal.gunStop();
        this.autoStopBtn.visible = false;
        },this,100)
    }

    private autoStopBtnEndEvent(evt:egret.Event)
    {
        // this.autoStopBtn.scaleX = 1;
        // this.autoStopBtn.scaleY = 1;
        // this.context.gunDeal.gunStop();
        // this.moreUI.context.soundctr.playSound("click_mp3");
    }


}